import express from 'express'
import bodyParser from 'body-parser'
import compression from 'compression'
import cors from 'cors'
import timeout from 'connect-timeout';
import config from './config/globalConfig';

const app = express();

// 超时时间
const TIME_OUT = 30 * 1e3;

// 设置超时 返回超时响应
app.use(timeout(TIME_OUT));
app.use((req, res, next) => {
    if (!req.timedout) next();
});


// 静态资源路径
app.use('/', express.static('static'));

//允许跨域
app.use(cors());
//数据压缩
app.use(compression());

// 解析请求体
app.use([
    bodyParser.json(),
    bodyParser.urlencoded({
        extended: true
    })
]);


// 初始化路由
import {createRouter} from './routes';

const router = createRouter();
app.use('/api/', router);

//启动程序，监听端口
app.listen(config.port, () => console.log(`app listening on port ${config.port}!!`));
